﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace GeniusAdminWebApplication
{
    public partial class department : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<Department> dl = getDepartmentList();
            foreach (Department d in dl)
            {
                Panel p_item = new Panel();
                p_item.CssClass = "department_item";
                p_item.Controls.Add(new LiteralControl("<span class='department_id'>" + d.id + "</span>"));
                p_item.Controls.Add(new LiteralControl("<span class='department_name'>" + d.name + "</span>"));
                p_item.Controls.Add(new LiteralControl("<span class='department_action'><input type='button' value='修改' onclick='modifyDepartment(" + d.id + ",\"" + d.name + "\");'/></span>"));
                ph_department.Controls.Add(p_item);
            }
        }

        private IList<Department> getDepartmentList()
        {
            DepartmentService ds = new DepartmentService();
            return ds.getDepartmentList();
        }

        [WebMethod]
        public static string ajaxAddDepartment(string name)
        {
            DepartmentService ds = new DepartmentService();
            Hashtable ht = ds.addDepartment(name);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            new SystemLogService().saveSystemLog((User)HttpContext.Current.Session["admin"], "添加部门：" + name);
            
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        [WebMethod]
        public static string ajaxModifyDepartment(int id, string newName)
        {
            DepartmentService ds = new DepartmentService();
            Hashtable ht = ds.modifyDepartment(id, newName);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        [WebMethod]
        public static string ajaxGetDepartmentList(int id)
        {
            DepartmentService ds = new DepartmentService();
            IList<Department> dl = ds.getDepartmentList();

            string result = "";
            foreach (Department d in dl)
            {
                result += d.id + "#" + d.name + "|";
            }
            return "{" +
                    "result:'" + result + "'" +
                    "}";
        }

    }
}